<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Models\Permissions;
use App\Http\Models\Roles;
use App\Http\Models\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\DB;

/**
 * 用户控制器
 */
class UserController extends Controller
{
    use AuthenticatesUsers;

    //用户信息
    protected $userInfo;

    public function __construct()
    {
        $this->userInfo = auth()->user();
    }

    /**
     *  用户列表，搜索数据
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList()
    {
        //获取所有按钮权限ID
        $request = request(['keywords', 'page', 'length']);

        $request['page'] = !empty($request['page']) ? $request['page'] : 1;
        $request['length'] = !empty($request['length']) ? $request['length'] : 10;
        $request['keywords'] = !empty($request['keywords']) ? $request['keywords'] : '';

        $where = ['page' => $request['page'], 'length' => $request['length'], 'keywords' => $request['keywords']];

        $data = User::getList($where);
        return respondWithData(0, $data, '请求成功');
    }

    /**
     * 获取所有用户
     * 用户组页面用到的接口
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAllUsers()
    {
        $where = ['status' => 1];
        $data = User::getUsers($where);
        return respondWithData(0, $data, '请求成功');
    }

    /**
     * 添加用户组
     * @return \Illuminate\Http\JsonResponse
     */
    public function add()
    {
        try {
            DB::beginTransaction();

            $request = request([ 'username', 'real_name', 'email', 'status', 'group_ids','password']);

            $model = new \App\Http\Models\User();
            //$model = $M::find($request['id']);
            $model->username = $request['username'];
            $model->real_name = $request['real_name'];
            $model->email = $request['email'];
            $model->status = !empty($request['status']) ? $request['status'] : 0;//  1：正常 2：禁用
            $model->password = !empty($request['password']) ? $request['password'] : '';

            $group_ids = trim($request['group_ids']) ? trim($request['group_ids']) : '';//组id
            if ($group_ids) {
                $group_ids = explode(',', $group_ids);
            } else {
                $group_ids = [];
            }

            if (!in_array($model->status, [1, 2])) {
                return respondWithData(0, [], '请求参数错误:status参数错误 ');
            }

            if (empty($model->username) || empty($model->real_name) || empty($model->email) ||  empty($model->password)) {
                return respondWithData(0, [], '请求参数错误:username,real_name,email,password 不能为空');
            }
            //判断是否重复
            $where = [
                'id' => $model->id,
                'username' => $model->username,
                'real_name' => $model->real_name,
                'email' => $model->email,
            ];
            $rt = $model::getListByWhere($where,0);
            if (!empty($rt)) {
                return respondWithData(0, [], '请求参数错误:username,real_name,email,不能重复 ');
            }

            //password
            $model->password= password_hash($model->password,PASSWORD_BCRYPT);
            $model->save();

            //保存组信息
            $model->syncRoles($group_ids);
            DB::commit();
            return respondWithData(0, [], '请求成功');
        } catch (\Exception $exception) {
            DB::rollBack();
            return respondWithData(0, [], '请求失败：' . $exception->getMessage());
        }

    }

    /**
     * 编辑用户组
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit()
    {
        try {
            DB::beginTransaction();
            $request = request(['id', 'username', 'real_name', 'email', 'status', 'group_ids']);

            $M = new \App\Http\Models\User();
            $model = $M::find($request['id']);
            $model->username = $request['username'];
            $model->real_name = $request['real_name'];
            $model->email = $request['email'];
            $model->status = !empty($request['status']) ? $request['status'] : 0;//  1：正常 2：禁用

            $group_ids = trim($request['group_ids']) ? trim($request['group_ids']) : '';//组id
            if ($group_ids) {
                $group_ids = explode(',', $group_ids);
            } else {
                $group_ids = [];
            }

            if (!in_array($model->status, [1, 2])) {
                return respondWithData(0, [], '请求参数错误:status参数错误 ');
            }

            if (empty($model->username) || empty($model->real_name) || empty($model->email)  ) {
                return respondWithData(0, [], '请求参数错误:username,real_name,email 不能为空');
            }
            //判断是否重复
            $where = [
                'id' => $model->id,
                'username' => $model->username,
                'real_name' => $model->real_name,
                'email' => $model->email,
            ];
            $rt = $M::getListByWhere($where);
            if (!empty($rt)) {
                return respondWithData(0, [], '请求参数错误:username,real_name,email,不能重复 ');
            }

            $model->save();

            //保存组信息
            $model->syncRoles($group_ids);
            DB::commit();
            return respondWithData(0, [], '请求成功');
        } catch (\Exception $exception) {
            DB::rollBack();
            return respondWithData(0, [], '请求失败：' . $exception->getMessage());
        }
    }


    /**
     * 删除用户
     * 软删除
     * 修改状态
     * @return void
     */
    public function editStatus()
    {
        $request = request(['id', 'status']);
        $M = new \App\Http\Models\User();
        $model = $M::find($request['id']);
        $model->status = !empty($request['status']) ? $request['status'] : 0;//  1：正常 2：禁用

        if (!in_array($model->status, [1, 2])) {
            return respondWithData(0, [], '请求参数错误:status参数错误 ');
        }
        $rt=$model->save();
        if ($rt) {
            return respondWithData(0, [], '请求成功');
        } else {
            return respondWithData(0, [], '请求失败');
        }
    }
}
